import { post } from '@/services/http'
import { constantRouterMap } from '@/router'
import { generator } from '@/utils/routerUtil'

const user = {
  state: {
    accessToken: "",
    clientId: "",
    userInfo: {},
    routers: constantRouterMap,
    addRouters: [],
    menuData: [],
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.accessToken = token
    },
    SET_CLIENT_ID: (state, id) => {
      state.clientId = id
    },
    SET_USER_INFO: (state, param) => {
      state.userInfo = param
    },
    SET_ROUTERS: (state, routers) => {
      state.routers = routers
    },
    SET_ADD_ROUTERS(state, addRouters) {
      state.addRouters = addRouters
    },
    SET_MENU_DATA(state, menuData) {
      state.menuData = menuData
    },
  },

  actions: {
    // 登录
    Login({ commit }, param) {
      return new Promise((resolve, reject) => {
        post("/permission/login", param).then(response => {
          const code = response.code
          if (code) {
            const error = {}
            error.response = response
            if (code && code !== 0 && code !== 200) {
              reject(error)
            }
          } else {
            const result = response.result
            commit('SET_TOKEN', result.accessToken)
            commit('SET_CLIENT_ID', result.clientId)
            commit('SET_ADD_ROUTERS', [])
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        let params = {};
        post("/sso/common/getSessionInfo", params).then(response => {
          const result = response.result
          commit('SET_USER_INFO', result)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        let params = {};
        params.accessToken = state.accessToken
        post("/sso/common/logout", params).then((response) => {
          location.reload();
          resolve(response)
        }).catch((error) => {
          reject(error)
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_CLIENT_ID', '')
          commit('SET_USER_INFO', null)
          commit('SET_ADD_ROUTERS', [])
        })
      })
    },
    //获取菜单
    GetRouterMenu({ commit }) {
      return new Promise((resolve, reject) => {
        post('/sso/common/dynamicMenu', { routerId: "" }).then((response) => {
          const result = response.result
          if (result && result.length > 0) {
            const routers = constantRouterMap.concat(generator(result))
            commit('SET_ADD_ROUTERS', result)
            commit('SET_ROUTERS', routers)
            resolve(routers)
          } else {
            resolve(result)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    setMenuData({ commit }, menuRoutes) {
      commit('SET_MENU_DATA', menuRoutes)
    }
  }
}

export default user
